<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - Lua 5.1.5 - src/lua.c</title>
  <link rel="stylesheet" type="text/css" href="../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../index.html">top level</a> - <a href="index.html">src</a> - lua.c</td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">Lua 5.1.5</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">155</td>
            <td class="headerCovTableEntry">234</td>
            <td class="headerCovTableEntryLo">66.2 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2019-06-13 19:44:15</td>
            <td></td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<span class="lineNum">       1 </span>            : /*
<span class="lineNum">       2 </span>            : ** $Id: lua.c,v 1.160.1.2 2007/12/28 15:32:23 roberto Exp $
<span class="lineNum">       3 </span>            : ** Lua stand-alone interpreter
<span class="lineNum">       4 </span>            : ** See Copyright Notice in lua.h
<span class="lineNum">       5 </span>            : */
<span class="lineNum">       6 </span>            : 
<span class="lineNum">       7 </span>            : 
<span class="lineNum">       8 </span>            : #include &lt;signal.h&gt;
<span class="lineNum">       9 </span>            : #include &lt;stdio.h&gt;
<span class="lineNum">      10 </span>            : #include &lt;stdlib.h&gt;
<span class="lineNum">      11 </span>            : #include &lt;string.h&gt;
<span class="lineNum">      12 </span>            : 
<span class="lineNum">      13 </span>            : #define lua_c
<span class="lineNum">      14 </span>            : 
<span class="lineNum">      15 </span>            : #include &quot;lua.h&quot;
<span class="lineNum">      16 </span>            : 
<span class="lineNum">      17 </span>            : #include &quot;lauxlib.h&quot;
<span class="lineNum">      18 </span>            : #include &quot;lualib.h&quot;
<span class="lineNum">      19 </span>            : 
<span class="lineNum">      20 </span>            : 
<span class="lineNum">      21 </span>            : 
<span class="lineNum">      22 </span>            : static lua_State *globalL = NULL;
<span class="lineNum">      23 </span>            : 
<span class="lineNum">      24 </span>            : static const char *progname = LUA_PROGNAME;
<span class="lineNum">      25 </span>            : 
<span class="lineNum">      26 </span>            : 
<span class="lineNum">      27 </span>            : 
<span class="lineNum">      28 </span><span class="lineNoCov">          0 : static void lstop (lua_State *L, lua_Debug *ar) {</span>
<span class="lineNum">      29 </span>            :   (void)ar;  /* unused arg. */
<span class="lineNum">      30 </span><span class="lineNoCov">          0 :   lua_sethook(L, NULL, 0, 0);</span>
<span class="lineNum">      31 </span><span class="lineNoCov">          0 :   luaL_error(L, &quot;interrupted!&quot;);</span>
<span class="lineNum">      32 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">      33 </span>            : 
<span class="lineNum">      34 </span>            : 
<span class="lineNum">      35 </span><span class="lineNoCov">          0 : static void laction (int i) {</span>
<span class="lineNum">      36 </span><span class="lineNoCov">          0 :   signal(i, SIG_DFL); /* if another SIGINT happens before lstop,</span>
<span class="lineNum">      37 </span>            :                               terminate process (default action) */
<span class="lineNum">      38 </span><span class="lineNoCov">          0 :   lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);</span>
<span class="lineNum">      39 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">      40 </span>            : 
<span class="lineNum">      41 </span>            : 
<span class="lineNum">      42 </span><span class="lineCov">          3 : static void print_usage (void) {</span>
<span class="lineNum">      43 </span><span class="lineCov">          3 :   fprintf(stderr,</span>
<span class="lineNum">      44 </span>            :   &quot;usage: %s [options] [script [args]].\n&quot;
<span class="lineNum">      45 </span>            :   &quot;Available options are:\n&quot;
<span class="lineNum">      46 </span>            :   &quot;  -e stat  execute string &quot; LUA_QL(&quot;stat&quot;) &quot;\n&quot;
<span class="lineNum">      47 </span>            :   &quot;  -l name  require library &quot; LUA_QL(&quot;name&quot;) &quot;\n&quot;
<span class="lineNum">      48 </span>            :   &quot;  -i       enter interactive mode after executing &quot; LUA_QL(&quot;script&quot;) &quot;\n&quot;
<span class="lineNum">      49 </span>            :   &quot;  -v       show version information\n&quot;
<span class="lineNum">      50 </span>            :   &quot;  --       stop handling options\n&quot;
<span class="lineNum">      51 </span>            :   &quot;  -        execute stdin and stop handling options\n&quot;
<span class="lineNum">      52 </span>            :   ,
<span class="lineNum">      53 </span>            :   progname);
<span class="lineNum">      54 </span><span class="lineCov">          3 :   fflush(stderr);</span>
<span class="lineNum">      55 </span><span class="lineCov">          3 : }</span>
<span class="lineNum">      56 </span>            : 
<span class="lineNum">      57 </span>            : 
<span class="lineNum">      58 </span><span class="lineCov">         10 : static void l_message (const char *pname, const char *msg) {</span>
<span class="lineNum">      59 </span><span class="lineCov">         10 :   if (pname) fprintf(stderr, &quot;%s: &quot;, pname);</span>
<span class="lineNum">      60 </span><span class="lineCov">         10 :   fprintf(stderr, &quot;%s\n&quot;, msg);</span>
<span class="lineNum">      61 </span><span class="lineCov">         10 :   fflush(stderr);</span>
<span class="lineNum">      62 </span><span class="lineCov">         10 : }</span>
<span class="lineNum">      63 </span>            : 
<span class="lineNum">      64 </span>            : 
<span class="lineNum">      65 </span><span class="lineCov">        294 : static int report (lua_State *L, int status) {</span>
<span class="lineNum">      66 </span><span class="lineCov">        294 :   if (status &amp;&amp; !lua_isnil(L, -1)) {</span>
<span class="lineNum">      67 </span><span class="lineCov">          7 :     const char *msg = lua_tostring(L, -1);</span>
<span class="lineNum">      68 </span><span class="lineCov">          7 :     if (msg == NULL) msg = &quot;(error object is not a string)&quot;;</span>
<span class="lineNum">      69 </span><span class="lineCov">          7 :     l_message(progname, msg);</span>
<span class="lineNum">      70 </span><span class="lineCov">          7 :     lua_pop(L, 1);</span>
<span class="lineNum">      71 </span>            :   }
<span class="lineNum">      72 </span><span class="lineCov">        294 :   return status;</span>
<span class="lineNum">      73 </span>            : }
<span class="lineNum">      74 </span>            : 
<span class="lineNum">      75 </span>            : 
<span class="lineNum">      76 </span><span class="lineCov">          6 : static int traceback (lua_State *L) {</span>
<span class="lineNum">      77 </span><span class="lineCov">          6 :   if (!lua_isstring(L, 1))  /* 'message' not a string? */</span>
<span class="lineNum">      78 </span><span class="lineCov">          2 :     return 1;  /* keep it intact */</span>
<span class="lineNum">      79 </span><span class="lineCov">          4 :   lua_getfield(L, LUA_GLOBALSINDEX, &quot;debug&quot;);</span>
<span class="lineNum">      80 </span><span class="lineCov">          4 :   if (!lua_istable(L, -1)) {</span>
<span class="lineNum">      81 </span><span class="lineNoCov">          0 :     lua_pop(L, 1);</span>
<span class="lineNum">      82 </span><span class="lineNoCov">          0 :     return 1;</span>
<span class="lineNum">      83 </span>            :   }
<span class="lineNum">      84 </span><span class="lineCov">          4 :   lua_getfield(L, -1, &quot;traceback&quot;);</span>
<span class="lineNum">      85 </span><span class="lineCov">          4 :   if (!lua_isfunction(L, -1)) {</span>
<span class="lineNum">      86 </span><span class="lineNoCov">          0 :     lua_pop(L, 2);</span>
<span class="lineNum">      87 </span><span class="lineNoCov">          0 :     return 1;</span>
<span class="lineNum">      88 </span>            :   }
<span class="lineNum">      89 </span><span class="lineCov">          4 :   lua_pushvalue(L, 1);  /* pass error message */</span>
<span class="lineNum">      90 </span><span class="lineCov">          4 :   lua_pushinteger(L, 2);  /* skip this function and traceback */</span>
<span class="lineNum">      91 </span><span class="lineCov">          4 :   lua_call(L, 2, 1);  /* call debug.traceback */</span>
<span class="lineNum">      92 </span><span class="lineCov">          4 :   return 1;</span>
<span class="lineNum">      93 </span>            : }
<span class="lineNum">      94 </span>            : 
<span class="lineNum">      95 </span>            : 
<span class="lineNum">      96 </span><span class="lineCov">        221 : static int docall (lua_State *L, int narg, int clear) {</span>
<span class="lineNum">      97 </span>            :   int status;
<span class="lineNum">      98 </span><span class="lineCov">        221 :   int base = lua_gettop(L) - narg;  /* function index */</span>
<span class="lineNum">      99 </span><span class="lineCov">        221 :   lua_pushcfunction(L, traceback);  /* push traceback function */</span>
<span class="lineNum">     100 </span><span class="lineCov">        221 :   lua_insert(L, base);  /* put it under chunk and args */</span>
<span class="lineNum">     101 </span><span class="lineCov">        221 :   signal(SIGINT, laction);</span>
<span class="lineNum">     102 </span><span class="lineCov">        221 :   status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);</span>
<span class="lineNum">     103 </span><span class="lineCov">        213 :   signal(SIGINT, SIG_DFL);</span>
<span class="lineNum">     104 </span><span class="lineCov">        213 :   lua_remove(L, base);  /* remove traceback function */</span>
<span class="lineNum">     105 </span>            :   /* force a complete garbage collection in case of errors */
<span class="lineNum">     106 </span><span class="lineCov">        213 :   if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);</span>
<span class="lineNum">     107 </span><span class="lineCov">        213 :   return status;</span>
<span class="lineNum">     108 </span>            : }
<span class="lineNum">     109 </span>            : 
<span class="lineNum">     110 </span>            : 
<span class="lineNum">     111 </span><span class="lineCov">          3 : static void print_version (void) {</span>
<span class="lineNum">     112 </span><span class="lineCov">          3 :   l_message(NULL, LUA_RELEASE &quot;  &quot; LUA_COPYRIGHT);</span>
<span class="lineNum">     113 </span><span class="lineCov">          3 : }</span>
<span class="lineNum">     114 </span>            : 
<span class="lineNum">     115 </span>            : 
<span class="lineNum">     116 </span><span class="lineCov">         57 : static int getargs (lua_State *L, char **argv, int n) {</span>
<span class="lineNum">     117 </span>            :   int narg;
<span class="lineNum">     118 </span>            :   int i;
<span class="lineNum">     119 </span><span class="lineCov">         57 :   int argc = 0;</span>
<span class="lineNum">     120 </span><span class="lineCov">        274 :   while (argv[argc]) argc++;  /* count total number of arguments */</span>
<span class="lineNum">     121 </span><span class="lineCov">         57 :   narg = argc - (n + 1);  /* number of arguments to the script */</span>
<span class="lineNum">     122 </span><span class="lineCov">         57 :   luaL_checkstack(L, narg + 3, &quot;too many arguments to script&quot;);</span>
<span class="lineNum">     123 </span><span class="lineCov">         57 :   for (i=n+1; i &lt; argc; i++)</span>
<span class="lineNum">     124 </span><span class="lineNoCov">          0 :     lua_pushstring(L, argv[i]);</span>
<span class="lineNum">     125 </span><span class="lineCov">         57 :   lua_createtable(L, narg, n + 1);</span>
<span class="lineNum">     126 </span><span class="lineCov">        274 :   for (i=0; i &lt; argc; i++) {</span>
<span class="lineNum">     127 </span><span class="lineCov">        217 :     lua_pushstring(L, argv[i]);</span>
<span class="lineNum">     128 </span><span class="lineCov">        217 :     lua_rawseti(L, -2, i - n);</span>
<span class="lineNum">     129 </span>            :   }
<span class="lineNum">     130 </span><span class="lineCov">         57 :   return narg;</span>
<span class="lineNum">     131 </span>            : }
<span class="lineNum">     132 </span>            : 
<span class="lineNum">     133 </span>            : 
<span class="lineNum">     134 </span><span class="lineCov">          1 : static int dofile (lua_State *L, const char *name) {</span>
<span class="lineNum">     135 </span><span class="lineCov">          1 :   int status = luaL_loadfile(L, name) || docall(L, 0, 1);</span>
<span class="lineNum">     136 </span><span class="lineCov">          1 :   return report(L, status);</span>
<span class="lineNum">     137 </span>            : }
<span class="lineNum">     138 </span>            : 
<span class="lineNum">     139 </span>            : 
<span class="lineNum">     140 </span><span class="lineCov">        112 : static int dostring (lua_State *L, const char *s, const char *name) {</span>
<span class="lineNum">     141 </span><span class="lineCov">        112 :   int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);</span>
<span class="lineNum">     142 </span><span class="lineCov">        109 :   return report(L, status);</span>
<span class="lineNum">     143 </span>            : }
<span class="lineNum">     144 </span>            : 
<span class="lineNum">     145 </span>            : 
<span class="lineNum">     146 </span><span class="lineCov">         53 : static int dolibrary (lua_State *L, const char *name) {</span>
<span class="lineNum">     147 </span><span class="lineCov">         53 :   lua_getglobal(L, &quot;require&quot;);</span>
<span class="lineNum">     148 </span><span class="lineCov">         53 :   lua_pushstring(L, name);</span>
<span class="lineNum">     149 </span><span class="lineCov">         53 :   return report(L, docall(L, 1, 1));</span>
<span class="lineNum">     150 </span>            : }
<span class="lineNum">     151 </span>            : 
<span class="lineNum">     152 </span>            : 
<span class="lineNum">     153 </span><span class="lineNoCov">          0 : static const char *get_prompt (lua_State *L, int firstline) {</span>
<span class="lineNum">     154 </span>            :   const char *p;
<span class="lineNum">     155 </span><span class="lineNoCov">          0 :   lua_getfield(L, LUA_GLOBALSINDEX, firstline ? &quot;_PROMPT&quot; : &quot;_PROMPT2&quot;);</span>
<span class="lineNum">     156 </span><span class="lineNoCov">          0 :   p = lua_tostring(L, -1);</span>
<span class="lineNum">     157 </span><span class="lineNoCov">          0 :   if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);</span>
<span class="lineNum">     158 </span><span class="lineNoCov">          0 :   lua_pop(L, 1);  /* remove global */</span>
<span class="lineNum">     159 </span><span class="lineNoCov">          0 :   return p;</span>
<span class="lineNum">     160 </span>            : }
<span class="lineNum">     161 </span>            : 
<span class="lineNum">     162 </span>            : 
<span class="lineNum">     163 </span><span class="lineNoCov">          0 : static int incomplete (lua_State *L, int status) {</span>
<span class="lineNum">     164 </span><span class="lineNoCov">          0 :   if (status == LUA_ERRSYNTAX) {</span>
<span class="lineNum">     165 </span>            :     size_t lmsg;
<span class="lineNum">     166 </span><span class="lineNoCov">          0 :     const char *msg = lua_tolstring(L, -1, &amp;lmsg);</span>
<span class="lineNum">     167 </span><span class="lineNoCov">          0 :     const char *tp = msg + lmsg - (sizeof(LUA_QL(&quot;&lt;eof&gt;&quot;)) - 1);</span>
<span class="lineNum">     168 </span><span class="lineNoCov">          0 :     if (strstr(msg, LUA_QL(&quot;&lt;eof&gt;&quot;)) == tp) {</span>
<span class="lineNum">     169 </span><span class="lineNoCov">          0 :       lua_pop(L, 1);</span>
<span class="lineNum">     170 </span><span class="lineNoCov">          0 :       return 1;</span>
<span class="lineNum">     171 </span>            :     }
<span class="lineNum">     172 </span>            :   }
<span class="lineNum">     173 </span><span class="lineNoCov">          0 :   return 0;  /* else... */</span>
<span class="lineNum">     174 </span>            : }
<span class="lineNum">     175 </span>            : 
<span class="lineNum">     176 </span>            : 
<span class="lineNum">     177 </span><span class="lineNoCov">          0 : static int pushline (lua_State *L, int firstline) {</span>
<span class="lineNum">     178 </span>            :   char buffer[LUA_MAXINPUT];
<span class="lineNum">     179 </span><span class="lineNoCov">          0 :   char *b = buffer;</span>
<span class="lineNum">     180 </span>            :   size_t l;
<span class="lineNum">     181 </span><span class="lineNoCov">          0 :   const char *prmt = get_prompt(L, firstline);</span>
<span class="lineNum">     182 </span><span class="lineNoCov">          0 :   if (lua_readline(L, b, prmt) == 0)</span>
<span class="lineNum">     183 </span><span class="lineNoCov">          0 :     return 0;  /* no input */</span>
<span class="lineNum">     184 </span><span class="lineNoCov">          0 :   l = strlen(b);</span>
<span class="lineNum">     185 </span><span class="lineNoCov">          0 :   if (l &gt; 0 &amp;&amp; b[l-1] == '\n')  /* line ends with newline? */</span>
<span class="lineNum">     186 </span><span class="lineNoCov">          0 :     b[l-1] = '\0';  /* remove it */</span>
<span class="lineNum">     187 </span><span class="lineNoCov">          0 :   if (firstline &amp;&amp; b[0] == '=')  /* first line starts with `=' ? */</span>
<span class="lineNum">     188 </span><span class="lineNoCov">          0 :     lua_pushfstring(L, &quot;return %s&quot;, b+1);  /* change it to `return' */</span>
<span class="lineNum">     189 </span>            :   else
<span class="lineNum">     190 </span><span class="lineNoCov">          0 :     lua_pushstring(L, b);</span>
<span class="lineNum">     191 </span><span class="lineNoCov">          0 :   lua_freeline(L, b);</span>
<span class="lineNum">     192 </span><span class="lineNoCov">          0 :   return 1;</span>
<span class="lineNum">     193 </span>            : }
<span class="lineNum">     194 </span>            : 
<span class="lineNum">     195 </span>            : 
<span class="lineNum">     196 </span><span class="lineNoCov">          0 : static int loadline (lua_State *L) {</span>
<span class="lineNum">     197 </span>            :   int status;
<span class="lineNum">     198 </span><span class="lineNoCov">          0 :   lua_settop(L, 0);</span>
<span class="lineNum">     199 </span><span class="lineNoCov">          0 :   if (!pushline(L, 1))</span>
<span class="lineNum">     200 </span><span class="lineNoCov">          0 :     return -1;  /* no input */</span>
<span class="lineNum">     201 </span>            :   for (;;) {  /* repeat until gets a complete line */
<span class="lineNum">     202 </span><span class="lineNoCov">          0 :     status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), &quot;=stdin&quot;);</span>
<span class="lineNum">     203 </span><span class="lineNoCov">          0 :     if (!incomplete(L, status)) break;  /* cannot try to add lines? */</span>
<span class="lineNum">     204 </span><span class="lineNoCov">          0 :     if (!pushline(L, 0))  /* no more input? */</span>
<span class="lineNum">     205 </span><span class="lineNoCov">          0 :       return -1;</span>
<span class="lineNum">     206 </span><span class="lineNoCov">          0 :     lua_pushliteral(L, &quot;\n&quot;);  /* add a new line... */</span>
<span class="lineNum">     207 </span><span class="lineNoCov">          0 :     lua_insert(L, -2);  /* ...between the two lines */</span>
<span class="lineNum">     208 </span><span class="lineNoCov">          0 :     lua_concat(L, 3);  /* join them */</span>
<span class="lineNum">     209 </span>            :   }
<span class="lineNum">     210 </span><span class="lineNoCov">          0 :   lua_saveline(L, 1);</span>
<span class="lineNum">     211 </span><span class="lineNoCov">          0 :   lua_remove(L, 1);  /* remove line */</span>
<span class="lineNum">     212 </span><span class="lineNoCov">          0 :   return status;</span>
<span class="lineNum">     213 </span>            : }
<span class="lineNum">     214 </span>            : 
<span class="lineNum">     215 </span>            : 
<span class="lineNum">     216 </span><span class="lineNoCov">          0 : static void dotty (lua_State *L) {</span>
<span class="lineNum">     217 </span>            :   int status;
<span class="lineNum">     218 </span><span class="lineNoCov">          0 :   const char *oldprogname = progname;</span>
<span class="lineNum">     219 </span><span class="lineNoCov">          0 :   progname = NULL;</span>
<span class="lineNum">     220 </span><span class="lineNoCov">          0 :   while ((status = loadline(L)) != -1) {</span>
<span class="lineNum">     221 </span><span class="lineNoCov">          0 :     if (status == 0) status = docall(L, 0, 0);</span>
<span class="lineNum">     222 </span><span class="lineNoCov">          0 :     report(L, status);</span>
<span class="lineNum">     223 </span><span class="lineNoCov">          0 :     if (status == 0 &amp;&amp; lua_gettop(L) &gt; 0) {  /* any result to print? */</span>
<span class="lineNum">     224 </span><span class="lineNoCov">          0 :       lua_getglobal(L, &quot;print&quot;);</span>
<span class="lineNum">     225 </span><span class="lineNoCov">          0 :       lua_insert(L, 1);</span>
<span class="lineNum">     226 </span><span class="lineNoCov">          0 :       if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)</span>
<span class="lineNum">     227 </span><span class="lineNoCov">          0 :         l_message(progname, lua_pushfstring(L,</span>
<span class="lineNum">     228 </span>            :                                &quot;error calling &quot; LUA_QL(&quot;print&quot;) &quot; (%s)&quot;,
<span class="lineNum">     229 </span>            :                                lua_tostring(L, -1)));
<span class="lineNum">     230 </span>            :     }
<span class="lineNum">     231 </span>            :   }
<span class="lineNum">     232 </span><span class="lineNoCov">          0 :   lua_settop(L, 0);  /* clear stack */</span>
<span class="lineNum">     233 </span><span class="lineNoCov">          0 :   fputs(&quot;\n&quot;, stdout);</span>
<span class="lineNum">     234 </span><span class="lineNoCov">          0 :   fflush(stdout);</span>
<span class="lineNum">     235 </span><span class="lineNoCov">          0 :   progname = oldprogname;</span>
<span class="lineNum">     236 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">     237 </span>            : 
<span class="lineNum">     238 </span>            : 
<span class="lineNum">     239 </span><span class="lineCov">         57 : static int handle_script (lua_State *L, char **argv, int n) {</span>
<span class="lineNum">     240 </span>            :   int status;
<span class="lineNum">     241 </span>            :   const char *fname;
<span class="lineNum">     242 </span><span class="lineCov">         57 :   int narg = getargs(L, argv, n);  /* collect arguments */</span>
<span class="lineNum">     243 </span><span class="lineCov">         57 :   lua_setglobal(L, &quot;arg&quot;);</span>
<span class="lineNum">     244 </span><span class="lineCov">         57 :   fname = argv[n];</span>
<span class="lineNum">     245 </span><span class="lineCov">         57 :   if (strcmp(fname, &quot;-&quot;) == 0 &amp;&amp; strcmp(argv[n-1], &quot;--&quot;) != 0) </span>
<span class="lineNum">     246 </span><span class="lineCov">          1 :     fname = NULL;  /* stdin */</span>
<span class="lineNum">     247 </span><span class="lineCov">         57 :   status = luaL_loadfile(L, fname);</span>
<span class="lineNum">     248 </span><span class="lineCov">         57 :   lua_insert(L, -(narg+1));</span>
<span class="lineNum">     249 </span><span class="lineCov">         57 :   if (status == 0)</span>
<span class="lineNum">     250 </span><span class="lineCov">         56 :     status = docall(L, narg, 0);</span>
<span class="lineNum">     251 </span>            :   else
<span class="lineNum">     252 </span><span class="lineCov">          1 :     lua_pop(L, narg);      </span>
<span class="lineNum">     253 </span><span class="lineCov">         52 :   return report(L, status);</span>
<span class="lineNum">     254 </span>            : }
<span class="lineNum">     255 </span>            : 
<span class="lineNum">     256 </span>            : 
<span class="lineNum">     257 </span>            : /* check that argument has no extra characters at the end */
<span class="lineNum">     258 </span>            : #define notail(x)       {if ((x)[2] != '\0') return -1;}
<span class="lineNum">     259 </span>            : 
<span class="lineNum">     260 </span>            : 
<span class="lineNum">     261 </span><span class="lineCov">         87 : static int collectargs (char **argv, int *pi, int *pv, int *pe) {</span>
<span class="lineNum">     262 </span>            :   int i;
<span class="lineNum">     263 </span><span class="lineCov">        169 :   for (i = 1; argv[i] != NULL; i++) {</span>
<span class="lineNum">     264 </span><span class="lineCov">        144 :     if (argv[i][0] != '-')  /* not an option? */</span>
<span class="lineNum">     265 </span><span class="lineCov">         56 :         return i;</span>
<span class="lineNum">     266 </span><span class="lineCov">         88 :     switch (argv[i][1]) {  /* option */</span>
<span class="lineNum">     267 </span><span class="lineCov">          3 :       case '-':</span>
<span class="lineNum">     268 </span><span class="lineCov">          3 :         notail(argv[i]);</span>
<span class="lineNum">     269 </span><span class="lineCov">          2 :         return (argv[i+1] != NULL ? i+1 : 0);</span>
<span class="lineNum">     270 </span><span class="lineCov">          1 :       case '\0':</span>
<span class="lineNum">     271 </span><span class="lineCov">          1 :         return i;</span>
<span class="lineNum">     272 </span><span class="lineNoCov">          0 :       case 'i':</span>
<span class="lineNum">     273 </span><span class="lineNoCov">          0 :         notail(argv[i]);</span>
<span class="lineNum">     274 </span><span class="lineNoCov">          0 :         *pi = 1;  /* go through */</span>
<span class="lineNum">     275 </span><span class="lineCov">          3 :       case 'v':</span>
<span class="lineNum">     276 </span><span class="lineCov">          3 :         notail(argv[i]);</span>
<span class="lineNum">     277 </span><span class="lineCov">          3 :         *pv = 1;</span>
<span class="lineNum">     278 </span><span class="lineCov">          3 :         break;</span>
<span class="lineNum">     279 </span><span class="lineCov">         27 :       case 'e':</span>
<span class="lineNum">     280 </span><span class="lineCov">         27 :         *pe = 1;  /* go through */</span>
<span class="lineNum">     281 </span><span class="lineCov">         80 :       case 'l':</span>
<span class="lineNum">     282 </span><span class="lineCov">         80 :         if (argv[i][2] == '\0') {</span>
<span class="lineNum">     283 </span><span class="lineCov">         77 :           i++;</span>
<span class="lineNum">     284 </span><span class="lineCov">         77 :           if (argv[i] == NULL) return -1;</span>
<span class="lineNum">     285 </span>            :         }
<span class="lineNum">     286 </span><span class="lineCov">         79 :         break;</span>
<span class="lineNum">     287 </span><span class="lineCov">          1 :       default: return -1;  /* invalid option */</span>
<span class="lineNum">     288 </span>            :     }
<span class="lineNum">     289 </span>            :   }
<span class="lineNum">     290 </span><span class="lineCov">         25 :   return 0;</span>
<span class="lineNum">     291 </span>            : }
<span class="lineNum">     292 </span>            : 
<span class="lineNum">     293 </span>            : 
<span class="lineNum">     294 </span><span class="lineCov">         84 : static int runargs (lua_State *L, char **argv, int n) {</span>
<span class="lineNum">     295 </span>            :   int i;
<span class="lineNum">     296 </span><span class="lineCov">        158 :   for (i = 1; i &lt; n; i++) {</span>
<span class="lineNum">     297 </span><span class="lineCov">         83 :     if (argv[i] == NULL) continue;</span>
<span class="lineNum">     298 </span>            :     lua_assert(argv[i][0] == '-');
<span class="lineNum">     299 </span><span class="lineCov">         83 :     switch (argv[i][1]) {  /* option */</span>
<span class="lineNum">     300 </span><span class="lineCov">         25 :       case 'e': {</span>
<span class="lineNum">     301 </span><span class="lineCov">         25 :         const char *chunk = argv[i] + 2;</span>
<span class="lineNum">     302 </span><span class="lineCov">         25 :         if (*chunk == '\0') chunk = argv[++i];</span>
<span class="lineNum">     303 </span>            :         lua_assert(chunk != NULL);
<span class="lineNum">     304 </span><span class="lineCov">         25 :         if (dostring(L, chunk, &quot;=(command line)&quot;) != 0)</span>
<span class="lineNum">     305 </span><span class="lineCov">          4 :           return 1;</span>
<span class="lineNum">     306 </span><span class="lineCov">         18 :         break;</span>
<span class="lineNum">     307 </span>            :       }
<span class="lineNum">     308 </span><span class="lineCov">         53 :       case 'l': {</span>
<span class="lineNum">     309 </span><span class="lineCov">         53 :         const char *filename = argv[i] + 2;</span>
<span class="lineNum">     310 </span><span class="lineCov">         53 :         if (*filename == '\0') filename = argv[++i];</span>
<span class="lineNum">     311 </span>            :         lua_assert(filename != NULL);
<span class="lineNum">     312 </span><span class="lineCov">         53 :         if (dolibrary(L, filename))</span>
<span class="lineNum">     313 </span><span class="lineCov">          2 :           return 1;  /* stop if file fails */</span>
<span class="lineNum">     314 </span><span class="lineCov">         51 :         break;</span>
<span class="lineNum">     315 </span>            :       }
<span class="lineNum">     316 </span><span class="lineCov">          5 :       default: break;</span>
<span class="lineNum">     317 </span>            :     }
<span class="lineNum">     318 </span>            :   }
<span class="lineNum">     319 </span><span class="lineCov">         75 :   return 0;</span>
<span class="lineNum">     320 </span>            : }
<span class="lineNum">     321 </span>            : 
<span class="lineNum">     322 </span>            : 
<span class="lineNum">     323 </span><span class="lineCov">         87 : static int handle_luainit (lua_State *L) {</span>
<span class="lineNum">     324 </span><span class="lineCov">         87 :   const char *init = getenv(LUA_INIT);</span>
<span class="lineNum">     325 </span><span class="lineCov">         87 :   if (init == NULL) return 0;  /* status OK */</span>
<span class="lineNum">     326 </span><span class="lineCov">         87 :   else if (init[0] == '@')</span>
<span class="lineNum">     327 </span><span class="lineNoCov">          0 :     return dofile(L, init+1);</span>
<span class="lineNum">     328 </span>            :   else
<span class="lineNum">     329 </span><span class="lineCov">         87 :     return dostring(L, init, &quot;=&quot; LUA_INIT);</span>
<span class="lineNum">     330 </span>            : }
<span class="lineNum">     331 </span>            : 
<span class="lineNum">     332 </span>            : 
<span class="lineNum">     333 </span>            : struct Smain {
<span class="lineNum">     334 </span>            :   int argc;
<span class="lineNum">     335 </span>            :   char **argv;
<span class="lineNum">     336 </span>            :   int status;
<span class="lineNum">     337 </span>            : };
<span class="lineNum">     338 </span>            : 
<span class="lineNum">     339 </span>            : 
<span class="lineNum">     340 </span><span class="lineCov">         87 : static int pmain (lua_State *L) {</span>
<span class="lineNum">     341 </span><span class="lineCov">         87 :   struct Smain *s = (struct Smain *)lua_touserdata(L, 1);</span>
<span class="lineNum">     342 </span><span class="lineCov">         87 :   char **argv = s-&gt;argv;</span>
<span class="lineNum">     343 </span>            :   int script;
<span class="lineNum">     344 </span><span class="lineCov">         87 :   int has_i = 0, has_v = 0, has_e = 0;</span>
<span class="lineNum">     345 </span><span class="lineCov">         87 :   globalL = L;</span>
<span class="lineNum">     346 </span><span class="lineCov">         87 :   if (argv[0] &amp;&amp; argv[0][0]) progname = argv[0];</span>
<span class="lineNum">     347 </span><span class="lineCov">         87 :   lua_gc(L, LUA_GCSTOP, 0);  /* stop collector during initialization */</span>
<span class="lineNum">     348 </span><span class="lineCov">         87 :   luaL_openlibs(L);  /* open libraries */</span>
<span class="lineNum">     349 </span><span class="lineCov">         87 :   lua_gc(L, LUA_GCRESTART, 0);</span>
<span class="lineNum">     350 </span><span class="lineCov">         87 :   s-&gt;status = handle_luainit(L);</span>
<span class="lineNum">     351 </span><span class="lineCov">         87 :   if (s-&gt;status != 0) return 0;</span>
<span class="lineNum">     352 </span><span class="lineCov">         87 :   script = collectargs(argv, &amp;has_i, &amp;has_v, &amp;has_e);</span>
<span class="lineNum">     353 </span><span class="lineCov">         87 :   if (script &lt; 0) {  /* invalid args? */</span>
<span class="lineNum">     354 </span><span class="lineCov">          3 :     print_usage();</span>
<span class="lineNum">     355 </span><span class="lineCov">          3 :     s-&gt;status = 1;</span>
<span class="lineNum">     356 </span><span class="lineCov">          3 :     return 0;</span>
<span class="lineNum">     357 </span>            :   }
<span class="lineNum">     358 </span><span class="lineCov">         84 :   if (has_v) print_version();</span>
<span class="lineNum">     359 </span><span class="lineCov">         84 :   s-&gt;status = runargs(L, argv, (script &gt; 0) ? script : s-&gt;argc);</span>
<span class="lineNum">     360 </span><span class="lineCov">         81 :   if (s-&gt;status != 0) return 0;</span>
<span class="lineNum">     361 </span><span class="lineCov">         75 :   if (script)</span>
<span class="lineNum">     362 </span><span class="lineCov">         57 :     s-&gt;status = handle_script(L, argv, script);</span>
<span class="lineNum">     363 </span><span class="lineCov">         70 :   if (s-&gt;status != 0) return 0;</span>
<span class="lineNum">     364 </span><span class="lineCov">         69 :   if (has_i)</span>
<span class="lineNum">     365 </span><span class="lineNoCov">          0 :     dotty(L);</span>
<span class="lineNum">     366 </span><span class="lineCov">         69 :   else if (script == 0 &amp;&amp; !has_e &amp;&amp; !has_v) {</span>
<span class="lineNum">     367 </span><span class="lineCov">          1 :     if (lua_stdin_is_tty()) {</span>
<span class="lineNum">     368 </span><span class="lineNoCov">          0 :       print_version();</span>
<span class="lineNum">     369 </span><span class="lineNoCov">          0 :       dotty(L);</span>
<span class="lineNum">     370 </span>            :     }
<span class="lineNum">     371 </span><span class="lineCov">          1 :     else dofile(L, NULL);  /* executes stdin as a file */</span>
<span class="lineNum">     372 </span>            :   }
<span class="lineNum">     373 </span><span class="lineCov">         69 :   return 0;</span>
<span class="lineNum">     374 </span>            : }
<span class="lineNum">     375 </span>            : 
<span class="lineNum">     376 </span>            : 
<span class="lineNum">     377 </span><span class="lineCov">         87 : int main (int argc, char **argv) {</span>
<span class="lineNum">     378 </span>            :   int status;
<span class="lineNum">     379 </span>            :   struct Smain s;
<span class="lineNum">     380 </span><span class="lineCov">         87 :   lua_State *L = lua_open();  /* create state */</span>
<span class="lineNum">     381 </span><span class="lineCov">         87 :   if (L == NULL) {</span>
<span class="lineNum">     382 </span><span class="lineNoCov">          0 :     l_message(argv[0], &quot;cannot create state: not enough memory&quot;);</span>
<span class="lineNum">     383 </span><span class="lineNoCov">          0 :     return EXIT_FAILURE;</span>
<span class="lineNum">     384 </span>            :   }
<span class="lineNum">     385 </span><span class="lineCov">         87 :   s.argc = argc;</span>
<span class="lineNum">     386 </span><span class="lineCov">         87 :   s.argv = argv;</span>
<span class="lineNum">     387 </span><span class="lineCov">         87 :   status = lua_cpcall(L, &amp;pmain, &amp;s);</span>
<span class="lineNum">     388 </span><span class="lineCov">         79 :   report(L, status);</span>
<span class="lineNum">     389 </span><span class="lineCov">         79 :   lua_close(L);</span>
<span class="lineNum">     390 </span><span class="lineCov">         79 :   return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;</span>
<span class="lineNum">     391 </span>            : }
<span class="lineNum">     392 </span>            : 
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.13</a></td></tr>
  </table>
  <br>

</body>
</html>
